package com.zelin.web.exception;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * ------------------------------
 * 功能：
 * 作者：WF
 * 微信：hbxfwf13590332912
 * 创建时间：2021/10/18-15:36
 * ------------------------------
 */
//@ControllerAdvice
public class GlobalExceptionHandler {

    //1. 处理MyException异常
    @ExceptionHandler(MyException.class)
    public String error(MyException ex, Model model){
        //1.1 定义异常信息
        String msg = "自定义异常！";
        //1.2 判断ex是否有值
        if(ex != null){
            msg = ex.getMessage();
        }
        //1.3 将错误信息放到model中
        model.addAttribute("msg",msg);
        //1.4 返回
        return "error";
    }

    //2. 处理其它异常
    @ExceptionHandler(Exception.class)
    public String exceptionError(Exception ex, Model model){
        //2.1 定义异常信息
        String msg = "未知异常！";
        //2.2 判断ex是否有值
        if(ex != null){
            msg = ex.getMessage();
        }
        //2.3 将错误信息放到model中
        model.addAttribute("msg",msg);
        //2.4 返回
        return "error";
    }
}
